﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Planer.Model;

namespace Planer.UI.Forms
{
    public partial class AddPeriodicalActionForm : AddIncomeOutcomeForm
    {
        public AddPeriodicalActionForm()
        {
            InitializeComponent();
        }

        public AddPeriodicalActionForm(ActionType ActionType, billing Bill)            
        {
            this.bill = Bill;
            this.actionType = ActionType;
            InitializeComponent();

            if (ActionType == Model.ActionType.OUTCOME)
            {                
                List<financial_goal> financicalGoals = FinancicalGoalModel.GetInstance().GetFinancicalGoalListByUser(Bill.user);
                if (financicalGoals.Count > 0)
                {                                    
                    comboBoxFinancicalGoals.Items.Add("Выберите накопление, если хотите");
                    comboBoxFinancicalGoals.Items.AddRange(financicalGoals.ToArray());
                    comboBoxFinancicalGoals.SelectedIndex = 0;
                    comboBoxFinancicalGoals.Enabled = true;
                }
            }

            CustomInitializeComponent(null);
        }

        protected override void CustomInitializeComponent(Model.action_category act)
        {
            base.CustomInitializeComponent(act);

            comboBoxMonth.Items.Add("");
            for (int i = 1; i < 13; i++)
                comboBoxMonth.Items.Add(i);
            comboBoxMonth.SelectedIndex = 0;

            comboBoxYear.Items.Add("");
            for (int i = 1; i < 6; i++)
                comboBoxYear.Items.Add(i);
            comboBoxYear.SelectedIndex = 0;

            comboBoxDay.Items.Add("");
            for (int i = 1; i < 30; i++)
                comboBoxDay.Items.Add(i);
            comboBoxDay.SelectedIndex = 1;

            dateTimePicker.MinDate = DateTime.Now;
            dateTimePicker.MaxDate = dateTimePicker.MinDate.AddMonths(1);

        }

        protected override void buttonOK_Click(object sender, EventArgs e)
        {
            int selected = comboBoxActionCategory.SelectedIndex;
            if (selected < 0) return;

            string summ = textBoxSumm.Text.Trim().Replace('.', ',');
            float money = 0;
            try
            {
                money = Convert.ToSingle(summ);
            }
            catch (Exception ex)
            {
                MessageBoxUtils.ShowErrorBox("Некорректно введена сумма");
                return;
            }

            if (actionType == ActionType.OUTCOME)
            {
                money = -money;
            }

            ModelResult mr = null;
            if (comboBoxFinancicalGoals.SelectedIndex > 0)
            {
                allocation alloc = new allocation()
                {
                    financial_goal = (financial_goal)comboBoxFinancicalGoals.SelectedItem,
                    amount = Math.Abs(money),
                    is_active = "y",
                    billing = bill,
                    create_date = DateTime.Now,
                    apply_date = dateTimePicker.Value,
                    years = comboBoxYear.SelectedIndex,
                    months = comboBoxMonth.SelectedIndex,
                    days = comboBoxDay.SelectedIndex
                };
                mr = AllocationModel.GetInstance().SaveOrUpdateAllocation(alloc);
            }
            else
            {
                periodical_action act = new periodical_action()
                {
                    action_category = (action_category)comboBoxActionCategory.SelectedItem,
                    amount = money,
                    apply_date = dateTimePicker.Value,
                    billing = bill,
                    create_date = DateTime.Now,
                    descr = textBoxDescription.Text,
                    is_active = "y",
                    years = comboBoxYear.SelectedIndex,
                    months = comboBoxMonth.SelectedIndex,
                    days = comboBoxDay.SelectedIndex
                };
                mr = PeriodicalActionModel.GetInstance().SaveOrUpdatePeriodicalAction(act);              
            }

            if (mr.Status == OperationStatusType.ERROR)
            {
                MessageBoxUtils.ShowErrorBox(mr.ShowAllMessages);
                return;
            }
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            Close();
        }

        private void comboBoxFinancicalGoals_SelectedIndexChanged(object sender, EventArgs e)
        {
            int index = comboBoxFinancicalGoals.SelectedIndex;
            if (index == 0)
            {
                label2.Enabled = true;
                comboBoxActionCategory.Enabled = true;
                buttonAddActionCategory.Enabled = true;
                label3.Enabled = true;
                textBoxDescription.Enabled = true;
            }
            else
            {
                label2.Enabled = false;
                comboBoxActionCategory.Enabled = false;
                buttonAddActionCategory.Enabled = false;
                label3.Enabled = false;
                textBoxDescription.Enabled = false;
            }
        }
    }
}
